//
//  RYYuMiddleView.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/12.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit
import SwiftDate

class RYYuMiddleView: SYBaseView , UITableViewDelegate, UITableViewDataSource {
    
    var titles:[String] = ["生产单价", "生产周期", "价格说明", "认养收获", "认养赠送"]
    var contents:[String] = []
    var tableView: UITableView!
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    public func configureView(_ model: RYProductDetailModel) {
        
//        let now = Date()
//        let nowStr = now.sy_toString(format: "yyyy.MM.dd")
//
//        let zhouqi = now + (model.productionCycle ?? 0).days
//        let zhouqiStr = zhouqi.sy_toString(format: "yyyy.MM.dd")
        
        let begain = model.productionCycleBegin?.replacingOccurrences(of: "-", with: ".") ?? ""
        let end = model.productionCycleEnd?.replacingOccurrences(of: "-", with: ".") ?? ""
        
        contents.append("\(model.unitPrice ?? "0")元/斤")
        contents.append("\(begain)-\(end)")
        contents.append(model.priceDescription ?? "")
        contents.append(model.adoptHarvest ?? "")
        contents.append(model.present ?? "")
        tableView.reloadData()
    }
    
    override func initViews() {
        
        setTableView()
        
        self.addSubview(tableView)
        self.addSubview(sepView)
        
        
        tableView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(CGFloat(titles.count)*41.0.autoWidth())
        }
        
        sepView.snp.makeConstraints { (make) in
            make.top.equalTo(tableView.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }

    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect.zero, style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView!.register(RYYuMiddleCell.self, forCellReuseIdentifier: "Cell")
        self.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: RYYuMiddleCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? RYYuMiddleCell
        cell?.selectionStyle = .none
        cell?.titleLb.text = titles[indexPath.row]
        if contents.count != 0 {
            cell?.contentLb.text  = contents[indexPath.row]
        }
        if indexPath.row == 4 {
            cell?.showPic()
        } else {
            cell?.hidePic()
        }
        return cell!
    }
   
  
}

class RYYuMiddleCell: SYBaseCell {
    
    let titleLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x999999)
        v.font = 13.autoFontSize()
        v.text = "产品单价"
        
        return v
    }()
    
    let contentLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x333333)
        v.font = 13.autoFontSize()
        v.text = "100.00元/斤"
        
        return v
    }()
    
    let pic: UIImageView = {
        let v: UIImageView = UIImageView()
        v.image = UIImage(named: "youhuiquan")
        return v
    }()
    
    func showPic() {
        pic.isHidden = false
        contentLb.snp.remakeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(titleLb.snp.right).offset(33.autoWidth())
            make.height.equalTo(20.autoWidth())
        }
    }
    func hidePic() {
        pic.isHidden = true
        contentLb.snp.remakeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(titleLb.snp.right).offset(8.autoWidth())
            make.height.equalTo(20.autoWidth())
        }
    }
    
    override func initViews() {
        pic.isHidden = true
        addSubview(titleLb)
        addSubview(contentLb)
        addSubview(pic)
        addSubview(lineView)
        titleLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15.autoWidth())
            make.height.equalTo(41.autoWidth())
            make.bottom.top.equalToSuperview()
        }
        contentLb.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(titleLb.snp.right).offset(8.autoWidth())
            make.height.equalTo(20.autoWidth())
        }
        pic.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(titleLb.snp.right).offset(8.autoWidth())
            make.height.equalTo(18.autoWidth())
            make.width.equalTo(21.autoWidth())
        }
        lineView.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(0.5.autoWidth())
        }
    }
}
